package com.sdruixinggroup.mondayb2b.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Base64;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;
import java.util.List;

import static android.content.Context.MODE_PRIVATE;

/**
 * Created by 00029935 on 2017/1/22.
 */

public class SharedPreferenceUtil {
    public static final String SP_HISTORY_NAME = "search_hostiry";
    public static final String SP_CUSTOMER_HISTORY_NAME = "search_customer_hostiry";
    public static final String SP_INSTALLMG_MAINLIST_HISTORY_NAME = "search_installmg_mainlist_hostiry";

    public static String List2String(List list)
            throws IOException {
        // 实例化一个ByteArrayOutputStream对象，用来装载压缩后的字节文件。
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        // 然后将得到的字符数据装载到ObjectOutputStream
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(
                byteArrayOutputStream);
        // writeObject 方法负责写入特定类的对象的状态，以便相应的 readObject 方法可以还原它
        objectOutputStream.writeObject(list);
        // 最后，用Base64.encode将字节文件转换成Base64编码保存在String中
        String SceneListString = new String(Base64.encode(
                byteArrayOutputStream.toByteArray(), Base64.DEFAULT));
        // 关闭objectOutputStream
        objectOutputStream.close();
        return SceneListString;

    }

    @SuppressWarnings("unchecked")
    public static List StringList(String SceneListString)
            throws StreamCorruptedException, IOException,
            ClassNotFoundException {
        byte[] mobileBytes = Base64.decode(SceneListString.getBytes(),
                Base64.DEFAULT);
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
                mobileBytes);
        ObjectInputStream objectInputStream = new ObjectInputStream(
                byteArrayInputStream);
        List SceneList = (List) objectInputStream
                .readObject();
        objectInputStream.close();
        return SceneList;
    }

    public static void saveList(Context context,List list){
      //  SharedPreferences sharedPreferences= PreferenceManager.getDefaultSharedPreferences(context);
        SharedPreferences sharedPreferences= context.getSharedPreferences(SP_HISTORY_NAME,MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        try {
            editor.putString("data",List2String(list));
            editor.commit();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void saveList(Context context,List list,String name){
        //  SharedPreferences sharedPreferences= PreferenceManager.getDefaultSharedPreferences(context);
        SharedPreferences sharedPreferences= context.getSharedPreferences(SP_HISTORY_NAME,MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        try {
            editor.putString(name,List2String(list));
            editor.commit();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static  List gainList(Context context){
        SharedPreferences sharedPreferences= context.getSharedPreferences(SP_HISTORY_NAME,MODE_PRIVATE);
        String data = sharedPreferences.getString("data","");
        List  list = null;
        try {
              list = StringList(data);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return list;
    }

    public static  List gainList(Context context,String name){
        SharedPreferences sharedPreferences= context.getSharedPreferences(SP_HISTORY_NAME,MODE_PRIVATE);
        String data = sharedPreferences.getString(name,"");
        List  list = null;
        try {
            list = StringList(data);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return list;
    }




}
